package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.BigDecimalCodec;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.GzdsQuestionInfo;
import com.ruoyi.system.domain.bo.GzdsGradeBO;
import com.ruoyi.system.handler.GzdsGradeHandler;
import com.ruoyi.system.utils.CatchUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GzdsGrade;
import com.ruoyi.system.service.IGzdsGradeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.xml.stream.events.StartDocument;

/**
 * 成绩管理Controller
 *
 * @author ruoyi
 * @date 2021-04-20
 */
@RestController
@RequestMapping("/system/grade")
public class GzdsGradeController extends BaseController
{
    @Autowired
    private IGzdsGradeService gzdsGradeService;

    @Autowired
    private GzdsGradeHandler gzdsGradeHandler;

    /**
     * 查询成绩管理列表
     */
    @ApiOperation("成绩查询")
    @GetMapping("/list")
    public TableDataInfo list(GzdsGrade gzdsGrade)
    {
        startPage();
        List<GzdsGrade> list = gzdsGradeService.selectGzdsGradeList(gzdsGrade);
        return getDataTable(list);
    }

    /**
     * 导出成绩管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:grade:export')")
    @Log(title = "成绩管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GzdsGrade gzdsGrade)
    {
        List<GzdsGrade> list = gzdsGradeService.selectGzdsGradeList(gzdsGrade);
        ExcelUtil<GzdsGrade> util = new ExcelUtil<GzdsGrade>(GzdsGrade.class);
        return util.exportExcel(list, "成绩管理数据");
    }

    /**
     * 获取成绩管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:grade:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") Long code)
    {
        return AjaxResult.success(gzdsGradeService.selectGzdsGradeById(code));
    }

    /**
     * 新增成绩管理
     */
    @Log(title = "成绩管理", businessType = BusinessType.INSERT)
    @ApiOperation("基本信息添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GzdsGrade gzdsGrade)
    {
        return toAjax(gzdsGradeService.insertGzdsGrade(gzdsGrade));
    }

    /**
     * 修改成绩管理
     */
    @PreAuthorize("@ss.hasPermi('system:grade:edit')")
    @Log(title = "成绩管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@RequestBody GzdsGrade gzdsGrade)
    {
        return toAjax(gzdsGradeService.updateGzdsGrade(gzdsGrade));
    }

    /**
     * 删除成绩管理
     */
    @PreAuthorize("@ss.hasPermi('system:grade:remove')")
    @Log(title = "成绩管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable Long[] codes)
    {
        return toAjax(gzdsGradeService.deleteGzdsGradeByIds(codes));
    }

    /**
     * 新增基本信息管理
     */
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/addbasic")
    @ApiOperation("基本信息添加")
    public AjaxResult addbasic(GzdsGrade gzdsGrade) {
        if (gzdsGradeService.selectGzdsGradeNameNumber(gzdsGrade.getBanklevel(), gzdsGrade.getName(), gzdsGrade.getPhoneNumber()).size() > 0) {
            return AjaxResult.error("msg", "竞赛成绩已生效");
        }
        else {
            if ("复赛".equals(gzdsGrade.getBanklevel())) {
                String banklevel = "初赛";
                if (gzdsGradeService.selectGzdsGradeNameNumber(banklevel, gzdsGrade.getName(), gzdsGrade.getPhoneNumber()).size() > 0) {
                    gzdsGrade.setPerformance("0");
                    gzdsGradeService.insertGzdsGrade(gzdsGrade);
                    List<GzdsGrade> gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(gzdsGrade.getBanklevel(), gzdsGrade.getName(), gzdsGrade.getPhoneNumber());
                    return AjaxResult.success("data", gzdsGradeList);
                }else {
                    return AjaxResult.error("msg", "未获得复赛资格");
                }
            } else if ("决赛".equals(gzdsGrade.getBanklevel())) {
                String banklevel = "复赛";
                if (gzdsGradeService.selectGzdsGradeNameNumber(banklevel, gzdsGrade.getName(), gzdsGrade.getPhoneNumber()).size() > 0) {
                    gzdsGrade.setPerformance("0");
                    gzdsGradeService.insertGzdsGrade(gzdsGrade);
                    List<GzdsGrade> gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(gzdsGrade.getBanklevel(), gzdsGrade.getName(), gzdsGrade.getPhoneNumber());
                    return AjaxResult.success("data", gzdsGradeList);
                }else {
                    return AjaxResult.error("msg", "未获得决赛资格");
                }
            } else if ("加赛".equals(gzdsGrade.getBanklevel())) {
                String banklevel = "复赛";
                if (gzdsGradeService.selectGzdsGradeNameNumber(banklevel, gzdsGrade.getName(), gzdsGrade.getPhoneNumber()).size() > 0) {
                    gzdsGrade.setPerformance("0");
                    gzdsGrade.setStartTime(new Date());
                    gzdsGradeService.insertGzdsGrade(gzdsGrade);
                    String value = Md5Utils.hash(gzdsGrade.getPhoneNumber());
                    CatchUtils.setCache(gzdsGrade.getPhoneNumber(), value);
                    List<GzdsGrade> gzdsGradeList = gzdsGradeService.selectGzdsGradeNameNumber(gzdsGrade.getBanklevel(), gzdsGrade.getName(), gzdsGrade.getPhoneNumber());
                    GzdsGrade grade = new GzdsGrade();
                    if (gzdsGradeList!=null && gzdsGradeList.size()>0) {
                        grade = gzdsGradeList.get(0);
                        grade.setValue(value);
                    }
                    return AjaxResult.success("data", grade);
                }else {
                    return AjaxResult.error("msg", "未获得加赛资格");
                }
            }
            else {
                return AjaxResult.error("msg", "操作异常");
            }
        }
    }



    /**
     * 新增成绩管理
     */
    @Log(title = "成绩管理", businessType = BusinessType.INSERT)
    @PostMapping("/addgrade")
    public AjaxResult addgrade(Long code, String banklevel, String answers )throws ParseException {
        int count = gzdsGradeHandler.addGrade(code, banklevel,JSONArray.parseArray(answers, GzdsQuestionInfo.class));
        if (count > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     *  成绩排行榜
     * @param bankLevel 考试等级
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getGradeRankList")
    public AjaxResult getGradeRankList(String bankLevel, Integer pageNum, Integer pageSize) {
        /** 查询数据 **/
        List<GzdsGradeBO> retList = gzdsGradeService.getGradeRankList(bankLevel, pageNum, pageSize);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("list", retList);
        retMap.put("count", gzdsGradeService.getGradeRankCount(bankLevel));

        /** 返回数据 **/
        return AjaxResult.success("data", retMap);
    }

    /**
     * 根据用户ID查询排名等基本信息
     * @param code
     * @param bankLevel
     * @return
     */
    @GetMapping("/getGradeRankByCode")
    public AjaxResult getGradeRankByCode(String code, String bankLevel) {
        /** 查询数据 **/
        GzdsGradeBO retBO = gzdsGradeService.getGradeRankByCode(code, bankLevel);

        /** 返回数据 **/
        return AjaxResult.success("data", retBO);
    }

    /**
     * 根据用户ID查询排名等基本信息
     * @param name
     * @param phoneNumber
     * @param banklevel
     * @return
     */
    @RequestMapping("/getGradeRankByName")
    public AjaxResult getGradeRankByName(String name, String phoneNumber, String banklevel) {
        /** 查询数据 **/
        GzdsGradeBO retBO = gzdsGradeService.getGradeRankByName(name, phoneNumber, banklevel);

        /** 返回数据 **/
        return AjaxResult.success("data", retBO);
    }
}
